﻿using System;
using System.Linq;
using System.Windows;
using BugLister;

namespace BugLister.Dialogs
{
    /// <summary>
    /// Interaction logic for AddBugDialog.xaml
    /// </summary>
    public partial class AddBugDialog : Window
    {
        internal BugEntry Bug { get; private set; }
        public AddBugDialog()
        {
            this.InitializeComponent();
            foreach (var type in Enum.GetNames(typeof(BugEntry.BugType)))
            {
                typeCombo.Items.Add(type);
            }
            foreach (var importance in Enum.GetNames(typeof(BugEntry.BugImportance)))
            {
                importanceCombo.Items.Add(importance);
            }
        }

        private void AddBtn_Click(object sender, RoutedEventArgs e)
        {
            if (AllFieldFilled)
            {
                Bug = new BugEntry();
                Bug.Importance = (BugEntry.BugImportance)Enum.Parse(typeof(BugEntry.BugImportance), importanceCombo.SelectedItem.ToString());
                Bug.Type = (BugEntry.BugType)Enum.Parse(typeof(BugEntry.BugType), typeCombo.SelectedItem.ToString());
                Bug.ShortDescription = shortDescText.Text;
                Bug.LongDescription = longDescText.Text;
                DialogResult = true;
            }
            else
                warningLabel.Content = "Not all items has value!";
        }

        private void CancelBtn_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = false;
        }

        public bool AllFieldFilled
        {
            get
            {
                return (!string.IsNullOrEmpty(shortDescText.Text) &&
                        !string.IsNullOrEmpty(longDescText.Text) &&
                        !string.IsNullOrEmpty((typeCombo.SelectedItem ?? string.Empty).ToString()) &&
                        !string.IsNullOrEmpty((importanceCombo.SelectedItem ?? string.Empty).ToString()));
            }
        }
    }
}